package com.mf.ergate.web.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * PjaxInterceptor
 * User: zhaoming
 * Date: 2018-09-15
 * To change this template use File | Settings | File Templates.
 **/
public class PjaxInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        Boolean pjax = Boolean.parseBoolean(request.getHeader("X-PJAX"));
        if(modelAndView != null){
//            modelAndView.addObject("layoutTemplet", "/common/layout/default_layout.ftl");
            logger.debug("Request uri: {}; isPjax: {};", request.getRequestURI(), pjax);
            if (pjax) {
                modelAndView.addObject("layoutTemplet", "/common/layout/pjax_layout.ftl");
            } else {
                modelAndView.addObject("layoutTemplet", "/common/layout/default_layout.ftl");
            }
        }
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
